layui.use(['layer', 'form', 'ztree', 'laydate', 'admin', 'ax', 'table', 'treetable'], function () {
    var layer = layui.layer;
    var form = layui.form;
    var $ZTree = layui.ztree;
    var $ax = layui.ax;
    var laydate = layui.laydate;
    var admin = layui.admin;
    var table = layui.table;
    var treetable = layui.treetable;

    /**
     * 分类管理表管理
     */
    var Classification = {
        tableId: "classificationTable"
    };

    /**
     * 初始化表格的列
     */
    Classification.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: '分类序号'},
            {field: 'name', sort: true, title: '名称'},
            {field: 'parentCode', sort: true, title: '级别',templet:function (d) {
                    if(d.parentCode==-1){
                        return '<span>顶级</span>';
                    }else{
                        return '<span>二级</span>';
                    }
                }},
            {field: 'level', sort: true, title: '类型',templet:function (d) {
                    if(d.level==1){
                        return '<span>分类</span>';
                    }else if(d.level==2){
                        return '<span>品牌</span>';
                    }
                }},
            /*{field: 'logPath', sort: true, title: '分类log地址'},
            {field: 'bgPath', sort: true, title: '背景图片'},*/
            {align: 'center', toolbar: '#tableBar', title: '操作'}
        ]];
    };

    /**
     * 点击查询按钮
     */
    Classification.search = function () {
        var queryData = {};
        queryData['condition'] = $("#condition").val();
        table.reload(Classification.tableId, {
            where: queryData, page: {curr: 1}
        });
    };

    /**
     * 弹出添加对话框
     */
    Classification.openAddDlg = function () {
        window.location.href = Feng.ctxPath + '/classification/add';
    };

    /**
     * 导出excel按钮
     */
    Classification.exportExcel = function () {
        var checkRows = table.checkStatus(Classification.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击编辑
     *
     * @param data 点击按钮时候的行数据
     */
    Classification.openEditDlg = function (data) {
        window.location.href = Feng.ctxPath + '/classification/edit?id=' + data.id;
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    Classification.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/classification/delete", function (data) {
                Feng.success("删除成功!");
                Classification.initTable(Classification.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };



    Classification.initTable = function (menuId, data) {
        return treetable.render({
            elem: '#' + Classification.tableId,
            url: Feng.ctxPath + '/classification/listTree',
            page: false,
            height: "full-98",
            cellMinWidth: 100,
            cols: Classification.initColumn(),
            treeColIndex: 2,
            treeSpid: "-1",
            treeIdName: 'id',
            treePidName: 'parentCode',
            treeDefaultClose: true,
            treeLinkage: true
        });
    };
    var tableResult = Classification.initTable(Classification.tableId);
    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        Classification.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        Classification.openAddDlg();
    });

    // 导出excel
    $('#btnExp').click(function () {
        Classification.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + Classification.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;
        //alert(JSON.stringify(obj))
        if (layEvent === 'edit') {
            Classification.openEditDlg(data);
        } else if (layEvent === 'delete') {
            Classification.onDeleteItem(data);
        }
    });
});
